//
//  GBHomepageTableview.m
//  PublicSentimentMonitorSystem
//
//  Created by 郭斌 on 15/11/4.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "GBHomepageTableview.h"
#import "GBHomepageTableViewCell.h"

@interface GBHomepageTableview () <UITableViewDataSource,UITableViewDelegate>
{
    
}
@end

@implementation GBHomepageTableview
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AddNewData:) name:@"AddNewH" object:nil];
        
        self.rowHeight = 40;
        self.tableFooterView = [[UIView alloc] init];
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[GBHomepageTableViewCell class] forCellReuseIdentifier:@"Cell"];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteWithCell:) name:@"DeleteTheKeyword2" object:nil];
        
        [self sendTheDataArr];
    }
    return self;
}

//#pragma mark - 添加新数据
//-(void)AddNewData:(NSNotification *)note
//{
//    NSDictionary *user = [note userInfo];
//    NSString *newNickname = [user objectForKey:@"AddNewHomepage"];
//    
//    [_dataArray insertObject:newNickname atIndex:0];
//    
//    //刷新数据
//    [self reloadData];
//    //发送新的数据
//    [self sendTheDataArr];
//}

#pragma mark - 删除按钮，刷新tableview
-(void)deleteWithCell:(NSNotification *)note
{
    NSDictionary *user = [note userInfo];
    int position = [[user objectForKey:@"deleteBtnTag"] intValue] - 1000;
    [_dataArray removeObjectAtIndex:position];
    [_dataIdArray removeObjectAtIndex:position];
    [self reloadData];
    //发送新的数据
    [self sendTheDataArr];
}

#pragma mark - 发送数据
-(void)sendTheDataArr
{
    //发送新的数据
    NSDictionary *userInfo11b = [[NSDictionary alloc] initWithObjectsAndKeys:_dataArray, @"deleteArray", _dataIdArray, @"deleteIdArray",nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NewArray2" object:nil userInfo:userInfo11b];
}

#pragma mark - tableview返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    GBHomepageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.titleLab.text = [NSString stringWithFormat:@"%@", _dataArray[indexPath.row]];
    cell.CancleBtn.tag = indexPath.row + 1000;
    
    //定制cell右按钮
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
